package ferp.android.tasks.center;

import android.content.Context;
import android.os.AsyncTask;
import ferp.android.managers.ProfileManager;
import ferp.center.client.Action;
import ferp.center.network.request.RequestReportGet;
import ferp.center.network.response.ResponseReportGet;
import ferp.core.game.Game;
import ferp.core.game.Settings;
import ferp.core.game.Statistics;
import ferp.core.log.Log;
import ferp.core.player.Profile;
import ferp.core.state.State;

/**
* User: olegs
* Date: 2/6/13 4:25 PM
*/
public class TaskProfileImport extends AsyncTask<Void, Void, ResponseReportGet>
{
  public interface Listener
  {
    void onProfileImported(boolean success);
  }

  public TaskProfileImport(Context context, Listener listener, long reportID, boolean fromCurrentPosition)
  {
    this.rm = ProfileManager.regular(context);
    this.tm = ProfileManager.tutorial(context);

    this.listener = listener;
    this.reportID = reportID;
    this.fromCurrentPosition = fromCurrentPosition;
  }

  @Override
  protected ResponseReportGet doInBackground(Void... voids)
  {
    try
    {
      RequestReportGet request = new RequestReportGet();
      request.id = reportID;

      ResponseReportGet response = Action.execute(Action.GAME_REPORT_GET, request, ResponseReportGet.class);

      if (response != null)
      {
        Profile.Games games = Game.gson.fromJson(response.snapshot, Profile.Games.class);

        if (games.initial.tutorial())
        {
          String id = String.valueOf(games.initial.scenario);
          tm.deleteProfile(id);

          Profile p = tm.newCurrentProfile(id);
          save(response, p, id, games, tm);
        }
        else
        {
          String nickname = String.valueOf(reportID);
          rm.deleteProfile(nickname);

          Profile p = rm.newCurrentProfile();
          save(response, p, nickname, games, rm);
        }

        return response;
      }
    }
    catch (Exception e)
    {
      Log.error(Action.TAG, e);
    }

    return null;
  }

  @Override
  protected void onPostExecute(ResponseReportGet response)
  {
    listener.onProfileImported(response != null);
  }

  private void save(ResponseReportGet response, Profile p, String nickname, Profile.Games games, ProfileManager pm) throws Exception
  {
    Settings settings = Game.gson.fromJson(response.settings, Settings.class);
    Statistics statistics = Game.gson.fromJson(response.statistics, Statistics.class);

    p.nickname = nickname;
    p.settings = settings;
    p.setStatistics(statistics);
    p.importGame(games, response.version);

    // set the proper starting state
    if (!fromCurrentPosition)
      p.game().set(State.replay);

    // prevent update of the original profile
    p.useCenter = false;
    p.centerId = 0;

    //p.settings.orientation = Settings.Orientation.PORTRAIT;

    // save
    pm.saveCurrentProfile(false);
  }

  private ProfileManager.Regular rm;
  private ProfileManager.Tutorial tm;
  private final Listener listener;
  private final long reportID;
  private final boolean fromCurrentPosition;
}
